using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class Login : Form
    {
        bool logged = false;
        public Login()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "Authenticating";
            if (!backgroundWorker1.IsBusy)
            backgroundWorker1.RunWorkerAsync();
            

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            retrieve r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            object[] res = r.login(textBox1.Text, textBox2.Text);

            if (res == null)
            {
                logged = false;
                MessageBox.Show("Invalid Login");

            }
            else
            {
                int sec = (int)res[1];
                ((Container)this.MdiParent).security = sec;
                ((Container)this.MdiParent).id = (int)res[0];
                ((Container)this.MdiParent).Accountname = res[2].ToString();
                logged = true;
                string s = "Welcome , " + res[2].ToString();
                switch (sec)
                {
                    case 1:
                        s += "\n You are Logged In As Reader";
                        break;
                    case 2:
                        s += "\n You are Logged In As Contributer";
                        break;
                    case 3:
                        s += "\n You are Logged In As Administrator";
                        break;
                }
                MessageBox.Show(s);
               
                
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label4.Text = "";
            if (logged)
            {
                this.Hide();
                ((Container)this.MdiParent).refresh();
                ((Container)this.MdiParent).showDefaultWindow();
                textBox2.Text = "";
                this.Dispose();
            }
        }
    }
}